package com.jingdong.dao.shoppingCart.impl;

import com.jingdong.dao.shoppingCart.ShoppingCartDao;
import com.jingdong.model.Shopping_cart;
import com.jingdong.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class ShoppingCartDaoImpl implements ShoppingCartDao {
    @Override
    public int save(Shopping_cart shopping_cart) {
        try{
            SqlSession sqlSession= MyBatisUtils.getSqlSession();
            int saveResult=sqlSession.insert("com.jingdong.shoppingCartModel.save",shopping_cart);
            sqlSession.commit();
            sqlSession.close();
            return saveResult;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Shopping_cart> cartList(Map<String,Long> map) {
        try{
            SqlSession sqlSession= MyBatisUtils.getSqlSession();
            List<Shopping_cart> cartList=sqlSession.selectList("com.jingdong.shoppingCartModel.list",map);
            sqlSession.close();
            return cartList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Shopping_cart getById(Map<String,Long> map) {
        try{
            SqlSession sqlSession= MyBatisUtils.getSqlSession();
            Shopping_cart shopping_cart=sqlSession.selectOne("com.jingdong.shoppingCartModel.getById",map);
            sqlSession.close();
            return shopping_cart;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(Shopping_cart shopping_cart) {
        try{
            SqlSession sqlSession= MyBatisUtils.getSqlSession();
            int updateResult=sqlSession.update("com.jingdong.shoppingCartModel.update",shopping_cart);
            sqlSession.commit();
            sqlSession.close();
            return updateResult;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
